package com.ddpyjqtd.thread.threadstop;

import java.util.concurrent.TimeUnit;

/**
 * 
 * @Description: 线程停止
 * 
 * @author: ddpyjqtd
 * @Date: 2019年5月13日
 *
 */
public class ThreadStop implements Runnable {
	// 必须使用volatile关键字，不然主线程修改子线程是不能保存马上可见
	volatile boolean isFinish = false;

	@Override
	public void run() {
		System.out.println("线程开始");
		while (!isFinish) {
		}
		System.out.println("线程结束");
	}

	public static void main(String[] args) {
		ThreadStop threadStop = new ThreadStop();
		Thread thread = new Thread(threadStop);
		thread.start();
		System.out.println("这里是主线程");
		try {
			TimeUnit.SECONDS.sleep(3);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("准备关闭子线程");
		threadStop.isFinish = true;

//		 thread.stop();
	}
}
